home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cpluspls / cpluspls.lst
File List  |  1988-11-02  |  3KB  |  146 lines

  1. /* C++ version of sieve */
  2.  
  3. #include <stdio.h>
  4.  
  5. const TRUE = 1;
  6. const FALSE = 0;
  7. const SIZE = 8190;
  8. const MAX_ITER = 100;
  9.  
  10. class primes {
  11.  
  12.   char* flags;
  13.   int size;
  14.   int count;
  15. public:
  16.   primes(int dsize) { size = dsize; count = 0; flags = new char[dsize]; }
  17.  ~primes() { size = 0; count = 0; delete flags; }
  18.   int count_primes()
  19.   {
  20.     int prime, k;
  21.  
  22.     count = 0;                              /* prime counter */
  23.     for (int i = 0; i <= size; i++)         /* set all flags true */
  24.         *(flags + i) = TRUE;
  25.     for (i = 0; i <= size; i++)
  26.     {
  27.         if (*(flags + i))                /* found a prime */
  28.         {
  29.            prime = i + i + 3;            /* twice index + 3 */
  30.            for (k = i + prime; k <= size; k += prime)
  31.               *(flags +k ) = FALSE;  /* kill all multiple */
  32.               count++;              /* primes found */
  33.        }
  34.     }
  35.  
  36.     return count;
  37.  }
  38.  
  39. };
  40.  
  41. main()
  42.     {
  43.      int result;
  44.      primes num(SIZE);
  45.  
  46.     printf ("%d iterations\n", MAX_ITER);
  47.     /* do program MAX_ITER times */
  48.     for (int iter = 1; iter <= MAX_ITER; iter++) 
  49.        result = num.count_primes();
  50.     printf("%d primes.\n", result);      /* primes found on 100th pass */
  51.  
  52.  }
  53.  
  54. /* end sieve */
  55.  
  56.  
  57. /* C++ version of complex math benchmark */
  58.  
  59. #include <stdio.h>
  60.  
  61. class complex 
  62. {
  63.     double real, imag;
  64.  
  65.   public:
  66.     complex(double xreal = 0.0, double ximag = 0.0) 
  67.     { 
  68.       real = xreal; 
  69.       imag = ximag; 
  70.     }
  71.     complex(complex &);
  72.     void print_cmplx();
  73.     complex operator =(complex &);
  74.     complex operator *(complex &);
  75.     complex operator /(complex &);
  76.     };
  77.  
  78.     complex::complex(complex &c)
  79.     {
  80.       real = c.real;
  81.       imag = c.imag;
  82.     }
  83.     
  84.     void complex::print_cmplx()
  85.     {
  86.       printf("%lf  + i %lf", real, imag);
  87.     }
  88.     
  89.     complex complex::operator = (complex &c)
  90.     {
  91.        real = c.real;
  92.        imag = c.imag;
  93.        return *this;
  94.     }
  95.  
  96.     complex complex::operator *(complex &c)
  97.     {
  98.       complex result;
  99.  
  100.       result.real = real * c.real - imag * c.imag;
  101.       result.imag = real * c.imag + c.real * imag;
  102.       return result;
  103.     }
  104.  
  105.     complex complex::operator /(complex &c)
  106.     {
  107.       complex result;
  108.       double denom;
  109.   
  110.       denom = c.real * c.real + c.imag * c.imag;
  111.       result.real = (real * c.real + imag * c.imag) / denom;
  112.       result.imag = (c.real * imag - real * c.imag) / denom;
  113.  
  114.       return result;
  115.     }
  116.  
  117. main ()
  118. {
  119.    const int count = 5000;
  120.  
  121.    complex a(10.0,10.0), b(2.0,2.0), c(0.0,0.0);
  122.  
  123.    for (int i = 0; i < count; ++i)
  124.    {
  125.      c = a * b;
  126.      c = c / a;
  127.      c = a * b;
  128.      c = c / a;
  129.      c = a * b;
  130.      c = c / a;
  131.      c = a * b;
  132.      c = c / a;
  133.      c = a * b;
  134.      c = c / a;
  135.      c = a * b;
  136.      c = c / a;
  137.      c = a * b;
  138.      c = c / a;
  139.    }
  140.    puts("Done!");
  141.    c.print_cmplx();
  142.    printf("\n\n");
  143. }
  144.  
  145.  
  146. /* end complex */